package org.kalai.unifypay.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.kalai.unifypay.Constant;
import org.kalai.unifypay.bean.Transflow;
import org.kalai.unifypay.chanpay.Chanpay;
import org.kalai.unifypay.util.CommonUtil;
import org.kalai.unifypay.util.DataDao;
import org.kalai.unifypay.util.StringUtil;

import com.alibaba.fastjson.JSON;

/**
 * 畅捷快捷支付(确认)
 * 
 * @author zsl-2016年10月15日
 *
 */
@WebServlet("/QuickPaymentConfirm")
public class QuickPaymentConfirm extends HttpServlet {

   private static final long   serialVersionUID = 1L;

   private static final Logger logger           = Logger.getLogger(QuickPaymentConfirm.class);

   /**
    * @see HttpServlet#HttpServlet()
    */
   public QuickPaymentConfirm() {
      super();
   }

   /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
    *      response)
    */
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      CommonUtil.responseObject(response, Constant.ERROR_REQEST);
   }

   /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
    *      response)
    */
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      Map<String, String> param = CommonUtil.validate(request, response);
      if (param != null) {// 验证通过 返回参数集合
         Map<String, Object> result = CommonUtil.createRespMap("10", "系统异常");
         
         String tranxSN = param.get("tranxSN");
         String verificationCode = param.get("verificationCode");
         // 判断参数非空
         if (StringUtil.isEmpty(tranxSN, verificationCode)) {
            logger.error(String.format("业务参数错误：tranxSN=%s, verificationCode=%s", tranxSN, verificationCode));
            throw new RuntimeException("#200-业务参数错误");
         }
         /* 判断订单号为支付中才能支付 */
         try {
            Transflow transflows = DataDao.getInstance().getTransflow(tranxSN);
            if (StringUtil.isNotEmpty(transflows)&&transflows.getStatus() != 1) {
               logger.error(String.format("不是支付中的订单：orderNo=%s", tranxSN));
               throw new RuntimeException("#213-不是支付中的订单");
            }
            result=Chanpay.quickPaymentConfirm(tranxSN, verificationCode);
            
         } catch (Exception e) {
            String message = e.getMessage();
            if (message != null && message.startsWith("#")) {
                result = CommonUtil.createRespMap(message.substring(1, message.indexOf("-")), message.substring(message.indexOf("-") + 1));
            } else {
                result = CommonUtil.createRespMap("10", "交易异常,请重新下单!");
                logger.error(String.format("[orderNo=%s]交易异常:%s)",param.get("orderNo"), e)); 
            }
            logger.error(String.format("[tranxSN=%s]畅捷快捷支付(确认)接口异常:%s", tranxSN,e));
         } finally {
            logger.info("交易返回：" + result);
            CommonUtil.responseObject(response, JSON.toJSONString(result));
         }
      }
   }
}
